package org.regotto.basics.dataStruct;

import java.util.Arrays;

/**
 * 模拟散列表
 */
public class Hash {

    static final int N = 1000, NULL = 0x3f3f3f3f;
    static int[] h = new int[N];
    static {
        Arrays.fill(h, NULL);
    }

    static int Q(int x) {
        int k = (x % N + N) % N;
        while (h[k] != NULL && h[k] != x) {
            k++;
            if (k == N) k = 0;
        }
        return k;
    }

    public static void main(String[] args) {
        int[] p = new int[]{1, 3, 4, 6, 5};
        for (int i = 0; i < p.length; i++) {
            int k = Q(p[i]);
            h[k] = p[i];
        }
        System.out.println(h[Q(p[2])]);
        System.out.println(h[Q(7)] == NULL);
    }

}
